import {get,post} from './request2.js';

//用code换取用户openid
export function uniLogin()
{
	return new Promise((resolve, reject) => {
		uni.showToast({
			icon:'loading'
		})
		uni.login({
			provider:'weixin',
			success: ({code,errMsg}) => {
				uni.hideLoading()
				if (errMsg === 'login:ok') {
					resolve(code);
				} else {
					uni.showToast({
						icon:'error',
						title:'登录失败',
						duration:3000,
					})
				}
			},
			fail: (res) => {
				uni.hideLoading()
				reject(res);
			}
		})
	});
}

//获取用户信息
export function uniGetUserProfile()
{
	return new Promise((resolve, reject) => {
		uni.showToast({
			icon:'loading'
		})
		uni.getUserProfile({
			desc:'获取用户头像和昵称进行自动登录',
			lang:'zh_CN',
			success: ({errMsg, userInfo}) => {
				uni.hideLoading()
				if (errMsg === 'getUserProfile:ok') {
					resolve(userInfo);
				} else {
					uni.showToast({
						icon:'error',
						title:'获取用户信息失败',
						duration:3000,
					})
				}
			},
			fail: (res) => {
				uni.hideLoading()
				reject(res);
			}
		})
	});
}